package com.weixing.mall.provider.support.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.provider.api.model.vo.GoodsPlaceVo;
import com.weixing.mall.provider.api.model.vo.GoodsSkuVo;
import com.weixing.mall.provider.api.model.vo.ItemVo;
import com.weixing.mall.provider.api.service.GoodsFeign;
import com.weixing.mall.provider.service.IGoodsService;
import com.weixing.mall.util.ThreadLocalMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class InDepotBillDataListener extends AnalysisEventListener<InDepotBillData> {
    private static final Logger LOGGER = LoggerFactory.getLogger(InDepotBillData.class);
    /**
     * 每隔500条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 500;
    List<InDepotBillData> list = new ArrayList<InDepotBillData>();

    @Autowired
    private IGoodsService goodsService;




    public InDepotBillDataListener() {
    }

    @Autowired
    public InDepotBillDataListener(IGoodsService goodsService) {
        this.goodsService = goodsService;


    }

    @Override
    public void invoke(InDepotBillData data,AnalysisContext context) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        LOGGER.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        LOGGER.info("{}条数据，开始查询数据！", list.size());
        /*不执行任何操作，直接返回上级进行操作*/
        ThreadLocalMap.put("excel_tmp",list);

    }
}